<template>
  <div class="app-container1">
    <el-card class="margin-bottom-10 margin-top-10">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-input v-model="dataForm.key" clearable placeholder="参数名" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getDataList()"
          >查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button
            type="success"
            @click="addOrUpdateHandle()"
          >新增</el-button>
          <el-button
            :disabled="dataListSelections.length <= 0"
            type="danger"
            @click="deleteHandle()"
          >批量删除
          </el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-card>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        stripe
        style="width: 100%"
        @selection-change="selectionChangeHandle"
      >
        <el-table-column
          align="center"
          header-align="center"
          type="selection"
          width="50"
        />
        <el-table-column
          align="center"
          mheader-align="center"
          label="头像"
          prop="avatar"
          mwidth="60"
        />
        <el-table-column
          align="center"
          header-align="center"
          label="用户名"
          prop="username"
        />
        <el-table-column
          align="center"
          header-align="center"
          label="真实姓名"
          prop="realname"
        />
        <!-- 0-女；1-男 -->
        <el-table-column
          align="center"
          header-align="center"
          label="性别"
          prop="gender"
        />
        <!-- 0：禁用   1：正常 -->
        <el-table-column
          align="center"
          header-align="center"
          label="状态"
          prop="status"
        />
        <el-table-column
          align="center"
          header-align="center"
          label="更新时间"
          prop="updateTime"
        />
        <el-table-column
          align="center"
          fixed="right"
          header-align="center"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="text"
              @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button>
            <el-divider direction="vertical" />
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
      />
    </el-card>

    <!-- 弹窗, 新增 / 修改 -->
    <user-form-dialog
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script>
import userFormDialog from './user-form-dialog.vue'
import { listUser } from '@/api/system/user'

export default {
  components: {
    userFormDialog
  },
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      listUser({
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key
      }).then(({ data }) => {
        if (data && data.records.length > 0) {
          this.dataList = data.records
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(row) {
      var ids = row
        ? [row.id]
        : this.dataListSelections.map(item => {
          return item.id
        })

      var names = row
        ? [row.realname]
        : this.dataListSelections.map(item => {
          return item.realname
        })

      this.$confirm(
        `确定对[${names.join(',')}]进行[${row ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl('/system/user/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.margin-bottom-10 {
  margin-bottom: 10px;
}
.margin-top-10 {
  margin-top: 10px;
}
</style>
